//
//  LoginViewController.m
//  MonitoringNetwork
//
//  Created by admin on 2017/11/29.
//  Copyright © 2017年 Shenzhen Xinwo Transport. All rights reserved.
//

#import "LoginViewController.h"
#import "NSString+File.h"
#import "MainViewController.h"
#import "WaveView.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UITextField *useAccountTextFiled;
@property (nonatomic,strong)UITextField *passWordtextFiled;
@property (nonatomic,copy)NSString *useAccount;
@property (nonatomic,copy)NSString *passWord;
@property (nonatomic,strong)UIButton *loginBtn;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.14f green:0.52f blue:0.87f alpha:1.00f];
    [self setUpUI];
    // 添加图层
    UIImage *backImage = [UIImage imageNamed:@"bg.png"];
    self.view.layer.contents = (__bridge id _Nullable)(backImage.CGImage);
    if (_msg) {
        [YJProgressHUD showMessage:_msg inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
    }

}
- (void)setMsg:(NSString *)msg
{
    _msg = msg;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}

- (void)setUpUI
{
    UIView *centerView = [UIView new];
    centerView.backgroundColor = [UIColor whiteColor];
    centerView.layer.cornerRadius = kHeightOfPt(8);
    centerView.clipsToBounds = YES;
    [self.view addSubview:centerView];

    _titleLabel = [UILabel new];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text = @"沃特玛创新联盟车辆监控系统";
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:MaxFontSize];
    [self.view addSubview:_titleLabel];

    _loginBtn = [UIButton new];
    _loginBtn.layer.cornerRadius = 22;
    _loginBtn.clipsToBounds = YES;
    _loginBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:LagerFontSize];
//    [_loginBtn setBackgroundColor:[UIColor colorWithRed:0.14f green:0.52f blue:0.87f alpha:1.00f]];
    UIImage *image = [UIImage imageWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"roundBtn"], 1) scale:[UIScreen mainScreen].scale];
    [_loginBtn setBackgroundImage:image forState:UIControlStateNormal];
//    [_loginBtn setBackgroundImage:[[UIImage imageNamed:@"round"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeTile] forState:UIControlStateNormal];
    [_loginBtn setContentMode:UIViewContentModeScaleAspectFill];

    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    
    [_loginBtn addTarget:self action:@selector(loginBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [centerView addSubview:_loginBtn];
    
    UIView *lineOneView = [UIView new];
    lineOneView.backgroundColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f];
    [centerView addSubview:lineOneView];

    UIView *lineTwoView = [UIView new];
    lineTwoView.backgroundColor = [UIColor colorWithRed:0.80f green:0.80f blue:0.80f alpha:1.00f];
    [centerView addSubview:lineTwoView];

    _useAccountTextFiled = [UITextField new];
    _useAccountTextFiled.keyboardType = UIKeyboardTypeDefault;
    _useAccountTextFiled.keyboardType = UIKeyboardTypeDefault;
    _useAccountTextFiled.delegate = self;
    _useAccountTextFiled.font = [UIFont systemFontOfSize:17];
    _useAccountTextFiled.textAlignment = NSTextAlignmentLeft;
    _useAccountTextFiled.placeholder = @"登录名";
    [centerView addSubview:_useAccountTextFiled];
    
    _passWordtextFiled = [UITextField new];
    _passWordtextFiled.keyboardType = UIKeyboardTypeDefault;
    _passWordtextFiled.delegate = self;
    _passWordtextFiled.font = [UIFont systemFontOfSize:17];
    _passWordtextFiled.textAlignment = NSTextAlignmentLeft;
    _passWordtextFiled.secureTextEntry = YES;
    _passWordtextFiled.placeholder = @"密码";
    [centerView addSubview:_passWordtextFiled];

    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(kWidthOfPt(-90));
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.centerY.centerY.mas_equalTo(self.view);
        make.height.mas_equalTo(280);
    }];

    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(centerView.mas_top).offset(-70);
    }];

    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(centerView.mas_left).offset(kWidthOfPt(78));
        make.right.mas_equalTo(centerView.mas_right).offset(kWidthOfPt(-78));
        make.bottom.mas_equalTo(centerView.mas_bottom).offset(-36);
        make.height.mas_equalTo(43);
    }];
    
    [lineOneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(centerView.mas_left).offset(kWidthOfPt(78));
        make.right.mas_equalTo(centerView.mas_right).offset(kWidthOfPt(-78));
        make.top.mas_equalTo(centerView.mas_top).offset(78);
        make.height.mas_equalTo(kHeightOfPt(2));
    }];
    
    [lineTwoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(centerView.mas_left).offset(kWidthOfPt(78));
        make.right.mas_equalTo(centerView.mas_right).offset(kWidthOfPt(-78));
        make.top.mas_equalTo(lineOneView.mas_top).offset(63);
        make.height.mas_equalTo(kHeightOfPt(2));
    }];
    
    [_useAccountTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lineOneView.mas_left).offset(kWidthOfPt(15));
        make.bottom.mas_equalTo(lineOneView.mas_top).offset(-11);
        make.right.mas_equalTo(lineOneView.mas_right).offset(kWidthOfPt(-15));
    }];
    
    [_passWordtextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lineOneView.mas_left).offset(kWidthOfPt(15));
        make.bottom.mas_equalTo(lineTwoView.mas_top).offset(-11);
        make.right.mas_equalTo(lineTwoView.mas_right).offset(kWidthOfPt(-15));
    }];
 
    WaveView *waveOneView = [[WaveView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 157, kScreenWidth, kScreenHeight)];
    waveOneView.isDouble = YES;
//    waveOneView.waveViewType = WaveViewTypeDefault;
    waveOneView.waveAmplitude = 60;
    waveOneView.firsetWaveColor  = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.3f];
    waveOneView.secondWaveColor  = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.2f];
    waveOneView.thirdWaveColor  = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.1f];
    waveOneView.waveSpeed = 1;
    [self.view insertSubview:waveOneView atIndex:0];
}

- (void)loginBtnDidClick:(UIButton *)sender
{
    [self.useAccountTextFiled resignFirstResponder];
    [self.passWordtextFiled resignFirstResponder];
    sender.enabled = NO;
    _useAccount = _useAccountTextFiled.text;
    _passWord = _passWordtextFiled.text;
    if (!(_useAccount.length>0)) {
        sender.enabled = YES;
        [YJProgressHUD showMessage:@"请输入账号" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    if (!(_passWord.length>0)) {
        sender.enabled = YES;
        [YJProgressHUD showMessage:@"请输入密码" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    
//    if (_passWord.length<6) {
//        sender.enabled = YES;
//        [YJProgressHUD showMessage:@"密码小于6位" inView:self.view];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [YJProgressHUD hide];
//        });
//        return;
//    }
    [YJProgressHUD showProgress:@"加载中..." inView:self.view];
    NSDictionary *param = @{
                            @"loginName":_useAccount,
                            @"password":[_passWord base64EncodedString]
                            };
    [YJProgressHUD hide];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_Login method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        sender.enabled = YES;
        [YJProgressHUD hide];
        NSDictionary *dic = responseObject;
        if ([dic[@"state"] integerValue] == 0) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ISLOGIN"];
            [[NSUserDefaults standardUserDefaults] setValue:_passWord forKey:@"PassWord"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"obj"][@"userName"] forKey:@"UserName"];
            MainViewController *mainVC = [MainViewController new];
            [UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
            [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
        }
        if (!([dic[@"state"] integerValue] == 0)&&!([dic[@"state"] integerValue] == 401)) {
            [YJProgressHUD showProgress:dic[@"msg"] inView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [YJProgressHUD hide];
                });
            }
    } failure:^(NSError * _Nonnull error) {
        sender.enabled = YES;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [YJProgressHUD hide];
    [self.useAccountTextFiled resignFirstResponder];
    [self.passWordtextFiled resignFirstResponder];
}
-(void)dealloc
{
    [YJProgressHUD hide];
    [self.useAccountTextFiled resignFirstResponder];
    [self.passWordtextFiled resignFirstResponder];
}
@end
